package com.fc.advbox.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.fc.advbox.common.ResponseJson;
import com.fc.advbox.controller.admin.BaseController;
import com.fc.advbox.dto.report.EquipmentRegion;
import com.fc.advbox.model.org.OrgMonthProfitOverview;
import com.fc.advbox.service.IEquipmentService;
import com.fc.advbox.service.IOrgMonthProfitService;
import com.fc.advbox.utils.DateTimeHelper;
import com.fc.advbox.utils.RRException;
import com.fc.advbox.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * Created by E7450 on 2018/8/10.
 */
@RestController
@RequestMapping("/api/report")
@Api(value="报表接口", tags={"报表接口"})
@Slf4j
public class ReportController extends BaseController{

    @Autowired
    private IEquipmentService equipmentService;

    @Autowired
    private IOrgMonthProfitService orgMonthProfitService;

    /**
     * 获取乙方组织的合同总信息
     */
    @PostMapping("/equipment")
    @ApiOperation(value = "获取设备地区报表", httpMethod="POST",
            notes = " REST 方式请求," +
                    "Request Params: <ul>" +
//                    "<li><i>必填参数: orgId: 乙方组织ID </i></li>" +
//                    "<li><i>选填参数: type: string AGENT:代理合同，CHANNEL:渠道合同，RENT：广告商租赁合同</i></li>" +
                    "</ul>")
    @RequiresPermissions("api:report:view")
    public ResponseJson doEquipmentRegionReport(@RequestBody JSONObject params){


        List<EquipmentRegion> regionReport = equipmentService.queryEquipmentRegionReport(params);
        return ResponseJson.success(regionReport);
    }

    /**
     * 获取乙方组织的合同总信息
     */
    @PostMapping("/org/profit/monthly")
    @ApiOperation(value = "获取获取一段时间内的月收益统计", httpMethod="POST",
            notes = " REST 方式请求," +
                    "Request Params: <ul>" +
                    "<li><i>选填参数: orgId: 组织ID </i></li>" +
                    "<li><i>选填参数: orgName: 组织名称，模糊查询 </i></li>" +
                    "<li><i>必填参数: startDate: 查询开始时间 格式：yyyy-MM-dd 例如 2018-06-19 </i></li>" +
                    "<li><i>选填参数: endDate: 默认当前时间 格式：yyyy-MM-dd 例如 2018-06-19</i></li>" +
                    "</ul>")
    @RequiresPermissions("api:report:view")
    public ResponseJson doQueryOrgMonthlyProfitReport(@RequestBody JSONObject params){

        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");

        if(StringUtils.isEmpty(startDate))
        {
            throw new RRException("开始时间不能为空");
        }

        if(StringUtils.isEmpty(endDate))
        {
            params.put("endDate", DateTimeHelper.getStringFromDate(new Date(), DateTimeHelper.PAYLOAD_DATEONLY));
        }

        List<OrgMonthProfitOverview> respList = orgMonthProfitService.queryOrgMonthlyProfitReport(params);
        return ResponseJson.success(respList);
    }


}
